<?php
namespace app\controllers;


use app\logic\GoodsLogic;
use app\common\corelib\LocalUpload;
use app\logic\PlayLogic;
use app\models\Area;
use yii;

class GoodsController extends BackendController
{

    public function actionIndex()
    {
        $query = Yii::$app->request->get('form', []);
        $page = Yii::$app->request->get('page', 1);
        $goodsLogic = new GoodsLogic();
        $this->data['breadcrumbs'] = '商品管理';
        return $this->render('index', [
            'goods' => $goodsLogic->getGoods($query, $page),
            'query' => $query
        ]);
    }

    public function actionEdit()
    {
        $goodsLogic = new GoodsLogic();
        if(!Yii::$app->request->post()){
            $id = Yii::$app->request->get('goods_id');
            $goods = $goodsLogic->getById($id);
            $this->data['breadcrumbs'] = ['商品管理' => U('goods/index'), $goods['goods_name'] => '', '更新' => ''];
            if(file_exists(dirname(dirname(__FILE__)).DIRECTORY_SEPARATOR.'web'.$goods['goods_pic'])){
                $pic = $goods['goods_pic'];
            }else{
                $pic = '/default.jpg';
            }
            $goods_pics = explode(',',$goods['goods_pics']);
            foreach($goods_pics AS &$value){
                if(!file_exists(dirname(dirname(__FILE__)).DIRECTORY_SEPARATOR.'web'.$value)){
                    $value = '/default_shop.jpg';
                }
            }
            $stock = $goodsLogic->getStock($id);
            $stockids = '';
            $zonedata = [];
            foreach($stock AS $k=>$v){

                $zone = $v['province_id'].','.$v['city_id'].','.$v['district_id'].','.$v['street_id'];
                if($stockids){
                    $stockids .= '|'.$zone;
                }else{
                    $stockids = $zone;
                }
                $zonedata[$k]['zonecode'] = $v['province_id'].$v['city_id'].$v['district_id'].$v['street_id'];
                $zonedata[$k]['zonename'] = $v['province_name'].$v['city_name'].$v['district_name'].$v['street_name'];

            }
            $playLogic = new PlayLogic();
            $play = $playLogic->getAllCard();
            $province = Area::find()->where(['region_level'=>1])->asArray()->all();
            return $this->render('edit', [
                'province'=>$province,
                'play'=>$play,
                'zonedata'=>$zonedata,
                'goods' => $goods,
                'pic' => $pic,
                'stockids'=>$stockids,
                'goods_pics'=>$goods_pics,
                'theme' => '/assets/',
                'actionUrl' => U('goods/edit'),
            ]);
        }else{
            $params = Yii::$app->request->post('form');
            $id = Yii::$app->request->post('goods_id');

            if($_FILES['goods_pic']['name']){
                LocalUpload::go_upload($_FILES['goods_pic']);
                $uploadresult = '/uploads/'.LocalUpload::$filedir.'/'.LocalUpload::$filename;
                $thumbs = LocalUpload::thumbs(100,100);
            }else{
                $uploadresult = '';
            }
            $logicRes = $goodsLogic->editById($id, $params,$uploadresult);
            if ($logicRes) {
                Yii::$app->getSession()->setFlash('success','更新商品成功');
                jump('goods/index');
            } else {
                Yii::$app->getSession()->setFlash('error','更新商品失败');
                $this->goBack(Yii::$app->request->referrer);
            }
        }

    }




    public function actionSaveStatus()
    {
        $id = Yii::$app->request->get('goods_id');
        $goodsLogic = new GoodsLogic();
        $logicRes = $goodsLogic->saveStatus($id);
        if ($logicRes) {
            Yii::$app->getSession()->setFlash('success','更改商品状态成功');
        } else {
            Yii::$app->getSession()->setFlash('error','更改商品状态失败');
        }
        return $this->redirect(U('goods/index'));
    }



    public function actionAdd()
    {
        if(!Yii::$app->request->post()){
            $this->data['breadcrumbs'] = ['商品管理' => U('goods/index'), ' 添加商品' => ''];
            $playLogic = new PlayLogic();
            $play = $playLogic->getAllCard();
            $province = Area::find()->where(['region_level'=>1])->asArray()->all();
            return $this->render('add', [
                'province'=>$province,
                'play'=>$play,
                'theme' => '/assets/',
                'actionUrl' => U('goods/add')
            ]);
        }else{
            $params = Yii::$app->request->post('form');
            $goodsLogic = new GoodsLogic();
            if($_FILES['goods_pic']['name']){
                LocalUpload::go_upload($_FILES['goods_pic']);
                $uploadresult = '/uploads/'.LocalUpload::$filedir.'/'.LocalUpload::$filename;
                $thumbs = LocalUpload::thumbs(100,100);
            }else{
                $uploadresult = '';
            }
            $logicRes = $goodsLogic->addGoods($params,$uploadresult);
            if ($logicRes) {
                Yii::$app->getSession()->setFlash('success','新增商品成功');
                jump('goods/index');
            } else {
                Yii::$app->getSession()->setFlash('error','新增商品失败');
                $this->goBack(Yii::$app->request->referrer);
            }
        }
    }

    public function getStock($id){

    }





}